var airPortPickJS = require('../tool/airPortPick/airPortPick.js');
var areapickJS = require('../tool/areaPick/areaPick.js');

Page({
	data: {
		title: '',
		phone: '',
		name: wx.getStorageSync('userName'),
		carType: '',
		array: [],
		index: 0,
		noteLength: 0,
		Hidden: false,
		userType: wx.getStorageSync('userType'),
		//地区选择器数据
		areaData: {
			current: 0,
			province: [],
			city: [],
			region: [],
			town: [],
			provinceObjects: [],
			cityObjects: [],
			regionObjects: [],
			townObjects: [],
			starAreaSelectedStr: '请选择省市区',
			endAreaSelectedStr: '请选择省市区',
			maskVisual: 'hidden',
			provinceName: '请选择',
			isDefault: true,
		},
		//机场选择器数据
		airPortpickData: {
			maskVisualAir: 'hidden'
		}
	},

	onLoad: function () {
		var that = this;
		var seatNumsArr = new Array();
		for (var i = 5; i < 66; i++) {
			seatNumsArr.push(i);
		}
		this.setData({
			seatNumsArr: seatNumsArr
		})

	},
	onShow: function () {
		// 页面显示
      var appInstance = getApp();
      console.log(appInstance);

      if(appInstance.globalData.hasLogin){
          //在数据重置之前判断，首次登录是否为true
          if(this.data.firstLogin){
              loadListData(this,1);
          }else{
              var phone2 = wx.getStorageSync('username');
              if(phone2 != this.data.phone){

                  //将保存的phone重新赋值为此时本地所保存的phone
                  this.data.phone = phone2;
                  this.setData({
                      list:[]
                  });
                  loadListData(this,1);
              }
          }

          this.setData({
              firstLogin: false,
              isLogin: false,
              loginError: false
          });
      }else{
          if(appInstance.globalData.hasLogout){
              this.setData({
                  firstLogin: true,
                  isLogin: false,
                  loginError: false
              });
          }
      }
	},
	//座位选择
	bindPickerChange: function (e) {
		console.log(e)
		var seatNumArr = this.data.array;
		console.log(this.data.array);
		var index = e.detail.value;
		console.log(index);
		this.setData({
			seatNum: seatNumArr[index]
		})
	},
	//日期选择		
	bindDatePickerChange: function (e) {

		if (e.currentTarget.id == "start-date") {
			this.setData({
				starDate: e.detail.value
			})

		} else if (e.currentTarget.id == "end-date") {
			this.setData({
				endDate: e.detail.value
			})

		} else if (e.currentTarget.id == "start-time") {
			this.setData({
				starTime: e.detail.value
			})

		} else if (e.currentTarget.id == "end-time") {
			this.setData({
				endTime: e.detail.value
			})
		}
	},

	//机场选择
	//先区别起始机场，和目的机场 根据id获取机场列表，更新机场列表，
	airPortPick: function (e) {
		airPortPickJS.airPortPickDoor(this, e);
	},
	//地区选择
	areaPickTap: function (e) {
		console.log('地区选择');
		areapickJS.areaPickDoor(this, e);
	},
	//机场选择点击事件
	airPortTapped: function (e) {
		console.log(e);
		var index = e.currentTarget.dataset.index;
		var airPortName = this.data.airPortpickData.AirPortArr[index];
		var key = this.data.airPortType;
		console.log('key', key, this.data);
		this.setData(JSON.parse('{"' + key + '":"' + airPortName + '"}'));
		airPortPickJS.cascadeDismissAir(this, e);
	},

	stopMove: function () {
		console.log('don‘t move');
	},

	textInput: function (e) {
		var length = e.detail.value.length;
		this.setData({ noteLength: length });
	},

	//发布
	checkForm: function (e) {
		var dic = e.detail.value;
		console.log('参数', dic)
		var companyName;
		var app = getApp();
		//开始日期处理，如果时间没有选择就直接拼接日期 
		var starDate;
		var endDate;
		if (!dic.title || dic.title.length == 0) {
			wx.showModal({
				title: '提示',
				content: '标题不能为空',
			})
			return;
		}
		if (!dic.linker || dic.linker.length == 0) {
			wx.showModal({
				title: '提示',
				content: '联系人不能为空',
			})

			return;
		}
		if (!dic.company && this.data.userType <= 2 && this.data.userType ) {
			wx.showModal({
				title: '提示',
				content: '公司名不能为空',
			})
			return;
		}

		if (!dic.starAreaSelectedStr || dic.starAreaSelectedStr.length == 0) {

			wx.showModal({
				title: '提示',
				content: '上车地点不能为空',
			})
			return;
		}
		if (!dic.endAreaSelectedStr || dic.endAreaSelectedStr.length == 0) {
			wx.showModal({
				title: '提示',
				content: '下车地点不能为空',
			})
			return;
		}

		if (!dic.phone || dic.phone.length == 0) {
			wx.showModal({
				title: '提示',
				content: '联系方式不能为空',
			})
			return;
		}
		if (!dic.infoType || dic.infoType.length == 0) {
			wx.showModal({
				title: '提示',
				content: '业务类型不能为空',
			})
			return;
		}
		if (dic.startTime.length > 0 && dic.startDate.length > 0) {
			starDate = dic.startDate + ' ' + dic.startTime;
		} else if (dic.startDate.length > 0) {
			starDate = dic.startDate;
		} else {
			wx.showModal({
				title: '提示',
				content: '开始日期不能为空',
			})
			return;
		}

		if (dic.endTime.length > 0 && dic.endDate.length > 0) {
			endDate = dic.endDate + ' ' + dic.endTime;
		} else if (dic.endDate.length > 0) {
			endDate = dic.endDate;
		} else {
			wx.showModal({
				title: '提示',
				content: '结束日期不能为空',
			})
			return;
		}
		if (dic.seatNum < 5){
			wx.showModal({
				title: '提示',
				content: '座位数不能小于5',
			})
			return;
		}

		console.log('开始时间', starDate, '结束时间', endDate);
		wx.showNavigationBarLoading();
		var upPlace = dic.starAreaSelectedStr;
		var upPlaceArr = new Array(); //定义一数组 
		upPlaceArr = upPlace.split(" "); //字符分割
		var upProvince = upPlaceArr[0];
		var upCity = upPlaceArr[1];
		var upDistrict = upPlaceArr[2];

		var endPlace = dic.endAreaSelectedStr;
		var endPlaceArr = new Array();
		endPlaceArr = endPlace.split(" ");
		var endProvince = endPlaceArr[0];
		var endCity = endPlaceArr[1];
		var endDistrict = endPlaceArr[2];
		var appInstance = getApp();
		var company;
		var linker;
		//乘客 导游 司机
		if (this.data.userType > 2) {
			company = '';
			linker = dic.linker;
		} else {
			company = dic.company;
			linker = dic.linker;
		}
		console.log('公司', company, '人员', linker);
		wx.showToast({
			title: '正在登录...',
			icon: 'loading',
			duration: 10000
		});
		wx.request({
			url: 'https://www.lanshuicar.com/Server/EmptyCarInfomation.ashx',
			data: {
				mtitle: dic.title,
				mcompanyname: company,	//发布公司名称 必填 如果是车公司和旅行社就填写公司名，个人就写名字（后期做处理）
				mconnector: linker, 	//联系人
				muptime: starDate,			//上车时间   必填
				mupLctprovince: upProvince,		//上车省份   必填
				muplctcity: upCity,			//上车城市   必填
				muplctdistrict: upDistrict,	//上车区县
				muplctairport: ' ', 		//上车机场
				mdntime: endDate,			//下车时间   必填
				mdnlctprovince: endProvince,	//下车省份   必填
				mdnlctcity: endCity, 			//下车城市   必填
				mdnlctdistrict: endDistrict,		//下车区县
				mdnlctairport: ' ',			//下车机场
				mseatnum: dic.seatNum,	//座位数
				personNum: dic.personNum,	//乘客人数
				mOption: dic.note,		//备注
				mCarType: dic.carType,    	//车型
				mispublic: '1',	 	//（是否公开：0匿名，1公开）
				mconnecttel: dic.phone,  	//联系方式   必填
				mtype: dic.infoType,    	//信息类型(a-空车/b-业务)   必填
				mpersonid: wx.getStorageSync('userInfo').Id,	//发布个人ID（乘客/司机/导游-专用字段）
				mpersontype: wx.getStorageSync('userType')	//发布人性质（5乘客/3司机/4导游/1车公司/2旅行社）

			},
			method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
			// header: {}, // 设置请求的 header
			success: function (res) {
				var code = res.data.code;
				if (code == 0) {
					console.log('发布成功', res.data);

					wx.showModal({
						title: '提示',
						content: '发布成功',
						success: function (res) {
							wx.navigateBack({
								delta: 2
							})
						}
					})
				} else if (code != 0) {
					wx.showModal({
						title: '提示',
						content: res.data.msg,
						success: function (res) {
							wx.navigateBack({
								delta: 2
							})
						}
					})
				}
			},
			fail: function (e) {
				// fail
				console.log(e);
			},
			complete: function () {
				// success
				wx.hideToast();
				wx.hideNavigationBarLoading();
				// complete
			}
		})
	}
})




